////TIME1308849681958
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(java.util);
var application = new Object();
var mapactual = new Array();
var tempmapactual = new Array();
var rowSize = 26;
var arraySize = rowSize * rowSize;
var masterIdCount = 0;
var DELAY = 3;
var tpfCount = 0;
/**
 * Any live cell with fewer than two live neighbours dies, as if caused by
 * under-population. Any live cell with two or three live neighbours lives on to
 * the next generation. Any live cell with more than three live neighbours dies,
 * as if by overcrowding. Any dead cell with exactly three live neighbours
 * becomes a live cell, as if by reproduction.
 * 
 */
application.setup = function() {

	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockX(
			false);
	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockY(
			false);
	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockZ(
			false);
	ScriptBlocksApplication.getInstance().getLevelProperties()
			.setLevelDescription("Game of Life\n");
	// Set it all to false;
	for (row = 0; row < rowSize; row++) {
		mapactual[row] = application.newRow(row);
	}
	// Set it all to false;
	for (row = 0; row < rowSize; row++) {
		tempmapactual[row] = application.newTempRow(row);
	}

	// Set masterBlock
	for (row = 0; row < rowSize; row++) {
		for (col = 0; col < rowSize; col++) {
			var value = application.getValue(row, col);
		}
	}

	application.pulsar();

}
application.blinker = function() {
	var row = rowSize / 2;
	var col = rowSize / 2;
	application.setValue(row, col - 1, true);
	application.setValue(row, col, true);
	application.setValue(row, col + 1, true);

}

application.pulsar = function() {
	var row = rowSize / 2;
	var col = rowSize / 2;
	// Top Right
	application.setValue(row, col - 1, true);
	application.setValue(row, col, true);
	application.setValue(row, col + 1, true);

	application.setValue(row - 1, col - 2, true);
	application.setValue(row - 2, col - 2, true);
	application.setValue(row - 3, col - 2, true);

	application.setValue(row - 5, col - 1, true);
	application.setValue(row - 5, col, true);
	application.setValue(row - 5, col + 1, true);

	application.setValue(row - 1, col + 3, true);
	application.setValue(row - 2, col + 3, true);
	application.setValue(row - 3, col + 3, true);

	// Bottom Right
	application.setValue(row + 2, col - 1, true);
	application.setValue(row + 2, col, true);
	application.setValue(row + 2, col + 1, true);

	application.setValue(row + 3, col - 2, true);
	application.setValue(row + 4, col - 2, true);
	application.setValue(row + 5, col - 2, true);

	application.setValue(row + 7, col - 1, true);
	application.setValue(row + 7, col, true);
	application.setValue(row + 7, col + 1, true);

	application.setValue(row + 3, col + 3, true);
	application.setValue(row + 4, col + 3, true);
	application.setValue(row + 5, col + 3, true);

	// Top Left
	application.setValue(row, col - 7, true);
	application.setValue(row, col - 6, true);
	application.setValue(row, col - 5, true);

	application.setValue(row - 1, col - 4, true);
	application.setValue(row - 2, col - 4, true);
	application.setValue(row - 3, col - 4, true);

	application.setValue(row - 5, col - 7, true);
	application.setValue(row - 5, col - 6, true);
	application.setValue(row - 5, col - 5, true);

	application.setValue(row - 1, col - 9, true);
	application.setValue(row - 2, col - 9, true);
	application.setValue(row - 3, col - 9, true);

	// Bottom Left
	application.setValue(row + 2, col - 7, true);
	application.setValue(row + 2, col - 6, true);
	application.setValue(row + 2, col - 5, true);

	application.setValue(row + 3, col - 4, true);
	application.setValue(row + 4, col - 4, true);
	application.setValue(row + 5, col - 4, true);

	application.setValue(row + 7, col - 7, true);
	application.setValue(row + 7, col - 6, true);
	application.setValue(row + 7, col - 5, true);

	application.setValue(row + 3, col - 9, true);
	application.setValue(row + 4, col - 9, true);
	application.setValue(row + 5, col - 9, true);

}

application.toad = function() {
	var row = rowSize / 2;
	var col = rowSize / 2;
	application.setValue(row, col - 1, true);
	application.setValue(row, col, true);
	application.setValue(row, col + 1, true);

	application.setValue(row + 1, col - 2, true);
	application.setValue(row + 1, col - 1, true);
	application.setValue(row + 1, col, true);

}

application.newRow = function(row) {
	var rowArray = new Array();
	for (col = 0; col < rowSize; col++) {
		// rowArray[col] = false;
		rowArray[col] = application.addNPC(new Vector3f(row * 2, 0.0, col * 2),
				++masterIdCount);
	}
	return rowArray;
}

application.newTempRow = function(row) {
	var rowArray = new Array();
	for (col = 0; col < rowSize; col++) {
		rowArray[col] = false;
	}
	return rowArray;
}

application.getValue = function(row, col) {
	return mapactual[row][col].isPowered();
}
application.setValue = function(row, col, value) {
	mapactual[row][col].updatePower(value);
}
application.loop = function(name, tpf) {
	if (tpfCount > DELAY) {

		application.testGeneration();
		tpfCount = 0;
	} else {
		tpfCount = tpfCount + tpf;
	}
}
application.testGeneration = function() {
	for (row = 0; row < rowSize; row++) {
		for (col = 0; col < rowSize; col++) {
			var value = application.getValue(row, col) ? 1 : 0;

			// print(value);
		}
		// println("");
	}
	// Copy Array
	for (row = 0; row < rowSize; row++) {
		for (col = 0; col < rowSize; col++) {
			var value = application.getValue(row, col) ? true : false;
			tempmapactual[row][col] = value;
		}
	}

	for (row = 0; row < rowSize; row++) {
		for (col = 0; col < rowSize; col++) {
			neighbours = 0;

			var north = row - 1;
			if (north > 0) {

				var neighbor = tempmapactual[north][col];
				if (neighbor) {
					// println("NORTH " + north + "," + col+ "of " + row +","
					// +col);
					neighbours++;
				}
			}
			var south = row + 1;
			if (south < rowSize) {
				var neighbor = tempmapactual[south][col];
				if (neighbor) {
					// println("SOUTH " + south + " " + col + "of " + row +","
					// +col);
					neighbours++;
				}
			}
			var west = col - 1;
			if (west > 0) {
				var neighbor = tempmapactual[row][west];
				if (neighbor) {

					// println("WEST " + row + " " + west+ "of " + row +","
					// +col);
					neighbours++;
				}
			}
			var east = col + 1;
			if (east < rowSize) {
				var neighbor = tempmapactual[row][east];
				if (neighbor) {
					// println("EAST" + row + " " + east+ "of " + row +","
					// +col);
					neighbours++;
				}
			}

			// NORTH WEST
			var north = row - 1;
			var west = col - 1;
			if (north > 0 && west > 0) {

				var neighbor = tempmapactual[north][west];
				if (neighbor) {
					// println("NORTH WEST " + north + "," + west+ "of " + row
					// +"," +col);
					neighbours++;
				}
			}

			// NORTH EAST
			var north = row - 1;
			var east = col + 1;
			if (north > 0 && east < rowSize) {

				var neighbor = tempmapactual[north][east];
				if (neighbor) {
					// println("NORTH EAST " + north + "," + east+ "of " + row
					// +"," +col);
					neighbours++;
				}
			}
			// SOUTH WEST
			var south = row + 1;
			var west = col - 1;
			if (south < rowSize && west > 0) {

				var neighbor = tempmapactual[south][west];
				if (neighbor) {
					// println("SOUTH WEST " + north + "," + west+ "of " + row
					// +"," +col);
					neighbours++;
				}
			}

			// SOUTH EAST
			var south = row + 1;
			var east = col + 1;
			if (south < rowSize && east < rowSize) {

				var neighbor = tempmapactual[south][east];
				if (neighbor) {
					// println(" SOUTH EAST " + north + "," + east+ "of " + row
					// +"," +col);
					neighbours++;
				}
			}

			// var northest = south + 1

			if (neighbours >= 4 || neighbours == 1 || neighbours == 0) {
				application.setValue(row, col, false);
			}
			if (neighbours == 3) {
				application.setValue(row, col, true);
			}
		}
	}

}

application.addNPC = function(vector3f, masterId) {

	// var masterId = System.currentTimeMillis();
	var masterBlock = new MasterBlock("masterBlock", masterId);
	masterBlock.init();
	masterBlock.setLocalTranslation(vector3f);
	ScriptBlocksApplication.getInstance().getRootNode()
			.attachChild(masterBlock);

//	var Block1992909979 = new Block("Block1992909979", masterId);
//	Block1992909979.setBlockSides(BlockSides.SIDE_ABOVE);
//	Block1992909979.setCustomFrequency(true);
//	Block1992909979.setScriptName("/blocks/PlusOne.js");
//	Block1992909979.init();
//	Block1992909979.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
//		masterBlock.attachChild(Block1992909979);
//	var Block1193736688 = new Block("Block1193736688", masterId);
//	Block1193736688.setBlockSides(BlockSides.SIDE_ABOVE);
//	Block1193736688.setCustomFrequency(true);
//	Block1193736688.setScriptName("/blocks/TwoX.js");
//	Block1193736688.init();
//	Block1193736688.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
//		Block1992909979.attachChild(Block1193736688);
//	
//	var DetachedBlock1909895466 = new DetachedBlock("DetachedBlock1909895466", masterId);
//	DetachedBlock1909895466.setBlockSides(BlockSides.SIDE_ABOVE);
//	DetachedBlock1909895466.init();
//	DetachedBlock1909895466.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
//	Block1193736688.attachChild(DetachedBlock1909895466);

	masterBlock.updatePower(false);
	// masterBlock.updateMasterId(masterId);
	ScriptBlocksApplication.getInstance().addMasterBlock(
			masterBlock.getMasterId(), masterBlock);

	return masterBlock;

}
